<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 *
 * @abstract 品牌管理
 * @author zxj
 *
 */
class brand extends MY_Controller {
	private $t_brand = 'maintain_brand';  //品牌表
	function __construct() {
		parent::__construct();
        //加载品牌模型
        $this->load->model('brand_model');
	}

	/**
	 * 品牌列表
	 */
	function index() {
        //获取表单数据
        $data = $formField = gp();
		//分页
		$this->load->library('pagination');
		$config['page_query_string'] = TRUE;
		$config['base_url'] = blang_url('brands/brand/index/?');
		$config['total_rows'] = $this->brand_model->countRows($this->t_brand);
		$config['per_page'] = 15;
		$config['uri_segment'] = config_item('enable_lang')?5:4;
		$page = max(1,intval($data['page']));
		$slimit = ($page - 1) * $config['per_page'];
		$this->pagination->initialize($config);
		$data['show_pages'] = $this->pagination->create_links();
		$num_page = ceil($config['total_rows']/$config['per_page']);
		$data['show_params'] = "总共{$config['total_rows']}条记录   {$config['per_page']}条/页   当前第{$page}/{$num_page}页";
		//查询
		$datalist = $this->brand_model->select($this->t_brand,$slimit,$config['per_page'],'brand_id desc');
		//重构数据
		foreach ($datalist as $key=>&$vary) {
            $vary['id'] = $vary['brand_id'];
		}
        unset($vary);
		$data['dataList'] = $datalist;
		//导出数据
		$data['outputUrl'] = blang_url('brands/brand/index/?outputExcel=true&page='.$page);
		$outputExcel = gp('outputExcel');  //是否导出
		if ($outputExcel) {
            $fieldData = array(
                'brand_id'=>'品牌ID','brand_name'=>'品牌中文名称','brand_name_en'=>'品牌英文名称',
                'merchant_account'=>'品牌商账号'
            );
            excel_output('品牌列表',$fieldData,$datalist);
		}else{
			//视图以及样式
			$this->load->view('defaults/brand_list',$data);
            $this->load->view('defaults/common/footer');

		}
	}

	/**
	 * 新增品牌
	 */
	function add(){
		//表单处理
		$data['post_type'] = __FUNCTION__;
		//插入数据
        $getData = gp();
		if ($getData['submits'] && $getData['brand_name']) {
			$this->brand_model->addOrSetBrand('',$getData);
			js_goto(blang_url('brands/brand/'),'新增品牌成功!');
		}else{
			//视图以及样式
			$this->load->view('defaults/brand_post',$data);
			$this->load->view('defaults/common/footer');
		}
	}

    /**
     * 修改品牌
     * @param string $id
     */
	function edit($id='') {
		//表单处理
		$data['post_type'] = __FUNCTION__;
		//接收数据
        $id = empty($id)?gp('id'):$id;
        $brandFirst = $this->brand_model->get_brand($id);
        $data['brandFirst'] = $brandFirst;
        //修改数据
        $getData = gp();
        if ($getData['submits'] && $id) {
            $this->brand_model->addOrSetBrand($id,$getData);
			js_goto(blang_url('brands/brand/'.__FUNCTION__.'/'.$id),'修改成功!');
		}else{
			//视图以及样式
			$this->breadcrumb->append_crumb(array('url'=>'', 'name'=>'修改品牌'));
            $this->load->view('defaults/brand_post',$data);
            $this->load->view('defaults/common/footer');
		}
	}



    /**
     * 删除品牌
     * @param string $id
     * @param string $page
     */
	function del($id='',$page='') {
        if($this->users['account']!='zlj'){
            js_goto('','缺少权限',1);
        }
		$page = intval($page);
		$this->common_model->delete($this->t_brand,array('brand_id'=>intval($id)));
		js_goto(blang_url('brands/brand/index/?page='.$page));
	}


}
